<?php
/*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/21
 * Time: 22:22
 */

namespace app\index\controller;

use \think\Db;

class Member extends Base
{
    public function active_card(){
        $card_no = input("card_no");
        if(strlen($card_no) != 32) $this->error("卡密长度不正确");
        Db::startTrans();
        try {
            $card = Db::name("cards")->where("card_no",$card_no)->find();
            if(!$card){
                Db::rollback();
                $this->error("未查询到相关卡密");
            }
            if($card['actived'] == 1) $this->error("此卡密已被使用，请换一个再试");
            $data = array();
            $data['actived'] = 1;
            $data['active_user'] = $this->user['id'];
            $data['active_time'] = time();
            if(Db::name("cards")->where("id",$card['id'])->update($data)){
                $member = Db::name("member_info")->where("user_id",$this->user['id'])->find();
                if(!$member){
                    $member = array();
                    $member['user_id'] = $this->user['id'];
                    $member['end_time'] = 0;
                    $member['vip_level'] = 0;
                    if(Db::name("member_info")->insert($member)){
                        $member['id'] = Db::name("member_info")->getLastInsID();
                    }else{
                        Db::rollback();
                        $this->error("未知错误，激活失败");
                    }
                }
                $endtime = $member['end_time']>time() ? $member['end_time'] : time();
                $member['end_time'] = $endtime + $card['use_time'];
                $member['vip_level'] = $member['vip_level'] == 0 ? 1 : $member['vip_level'];
                if(Db::name("member_info")->where("id",$member['id'])->update($member)){
                    Db::commit();
                    $this->success(NULL,"激活成功");
                }else{
                    Db::rollback();
                    $this->error("未知错误，激活失败");
                }
            }else{
                Db::rollback();
                $this->error("未知错误，激活失败");
            }
        } catch (\Exception $e) {
            Db::rollback();
            $this->error("未知错误，激活失败");
        }
    }

    public function collect(){
        $video = Db::name("videos")->where("id",intval(input("id")))->find();
        if(!$video) $this->error("视频不存在");
        $collect = Db::name("collect")->where("user_id",$this->user['id'])->where("video_id",$video['id'])->find();
        if($collect){
            if(Db::name("collect")->where("id",$collect['id'])->setField("isdelete",$collect['isdelete'] ? 0 : 1)){
                $collect['isdelete'] ? $this->success(1,"收藏成功") : $this->success(-1,"取消收藏成功");
            }else{
                $this->error("收藏设置失败");
            }
        }else{
            $data = array();
            $data['user_id'] = $this->user['id'];
            $data['video_id'] = $video['id'];
            if(Db::name("collect")->insert($data)){
                Db::name("videos")->where("id",$video['id'])->setInc("collect_times");
                $this->success(1,"收藏成功");
            }else{
                $this->error("收藏设置失败");
            }
        }
    }

    public function collect_list(){
        $user = $this->user;
        $db = Db::name("videos")->alias("v")->join("kl_collect c","v.id=c.video_id","LEFT")
            ->where('c.user_id',$user['id'])->where("isdelete",0)->field('v.id,title,img,address,play_times');

        $list = $db->order("c.id","desc")->paginate(100);
        $this->success($list->items());
    }

    public function getlinks(){
        $user = $this->user;
        $data = Db::name("links")->where("user_id",$user['parent_id'])->value("links");
        //如果没找到，则找管理员设置的发卡链接
        if(!$data){
            $user = Db::name("user")->where("username","admin")->where("user_type","admin")->find();
            $data = Db::name("links")->where("user_id",$user['id'])->value("links");
        }
        if($data){
            $this->success(unserialize($data));
        }else{
            $this->success();
        }
    }
}